package com.damie.web.mapper;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.damie.common.dao.WasteOrder;
import com.damie.web.vo.*;
import org.apache.ibatis.annotations.*;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ls
 * @since 2021-01-19
 */
public interface WasteOrderMapper extends BaseMapper<WasteOrder> {
    @Select("select * from t_waste_order ${ew.customSqlSegment}")
    Page<WasteOrder> getPageVo(Page<WasteOrder> iPage, QueryWrapper<WasteOrder> ew);

    @Select("SELECT \n" +
            "t1.waste_order_id,\n" +
            "t1.user_phone,\n" +
            "t1.user_address,\n" +
            "t1.appointment_time,\n" +
            "t1.waste_order_data,\n" +
            "t1.waste_order_message\n" +
            "FROM t_waste_order t1 \n" +
            "JOIN t_site t2\n" +
            "ON t1.site_id=t2.site_id ${ew.customSqlSegment}")
    Page<MyOrder> MyOrder(Page<MyOrder> iPage, QueryWrapper<MyOrder> ew);

    @Update("update t_waste_order set site_id=#{siteId},waste_order_status=#{wasteOrderStatus} where waste_order_id=#{wasteOrderId}")
    int orderTaking(WasteOrder wasteOrder);

    @Select("select waste_order_details_id,waste_name,waste_number,waste_price from t_waste_order_details where waste_order_id=#{wasteOrderId};")
    List<WasteOrderDetailsVo> findAllWasteOrderDetails(String wasteOrderId);

    @Select("SELECT \n" +
            "t1.address_name\n" +
            "FROM t_user_address t1 JOIN t_waste_order t2 \n" +
            "on t1.user_id=t2.user_id\n" +
            "WHERE t2.waste_order_id=#{wasteOrderId};")
    String addressName(String wasteOrderId);

//    @Select("SELECT \n" +
//            "t1.address_name,\n" +
//            "t2.user_phone,t2.appointment_time,t2.waste_order_data,t2.waste_order_message,\n" +
//            "t3.waste_name,t3.waste_number,t3.waste_price \n" +
//            "FROM t_user_address t1 JOIN t_waste_order t2 JOIN t_waste_order_details t3\n" +
//            "on t1.user_id=t2.user_id AND t2.waste_order_id=t3.waste_order_id \n" +
//            "WHERE t2.waste_order_id=#{wasteOrderId};")
//    @Results(id = "orderDetails",value = {
//            @Result(id=true,property = "wasteOrderId",column = "waste_order_id"),
//            @Result(property = "addressName",column = "address_name"),
//            @Result(property = "userPhone",column = "user_phone"),
//            @Result(property = "appointmentTime",column = "appointment_time"),
//            @Result(property = "wasteOrderData",column = "waste_order_data"),
//            @Result(property = "wasteOrderMessage",column = "waste_order_message"),
//            @Result(property = "wasteOrderDetailsVos",column = "waste_order_id",many = @Many(select = "com.damie.web.mapper.WasteOrderMapper.findAllWasteOrderDetails"))
//    })
//    OrderDetails findOne(String wasteOrderId);

//    @Update("update \n" +
//            "t_waste_order t1 join t_waste_order_details t2 \n" +
//            "ON t1.waste_order_id=t2.waste_order_id \n" +
//            "set \n" +
//            "t1.waste_order_status=#{wasteOrderStatus},t2.waste_number=#{wasteNumber},t2.waste_price=#{wastePrice} \n" +
//            "where t1.waste_order_id=#{wasteOrderId};")
//
//    int updateWasteOrder(WasteOrderVo wasteOrderVo);

    @Select("SELECT \n" +
            "t1.address_longitude,t1.address_latitude,\n" +
            "t3.site_longitude,t3.site_latitude \n" +
            "FROM t_user_address t1 JOIN t_waste_order t2 JOIN t_site t3\n" +
            "ON t1.address_id=t2.user_address_id AND t2.site_id=t3.site_id\n" +
            "WHERE t2.waste_order_id=#{wasteOrderId};")
    Distance findAddress(String wasteOrderId);


    @Update("update t_waste_order set waste_order_status=#{wasteOrderStatus} where waste_order_id=#{wasteOrderId};")
    void updateWasteOrder(WasteOrderVo wasteOrderVo);

    @Update("update t_waste_order_details set waste_number=#{wasteNumber},waste_price=#{wastePrice} " +
            "where waste_order_details_id=#{wasteOrderDetailsId};")
    void updateWasteOrderDetails(WasteOrderDetailsVo wasteOrderDetailsVo);


    @Select("select * from t_waste_order where waste_order_id=#{wasteOrderId}")
    WasteOrder findOne2(String wasteOrderId);

    @Update("update t_waste_order_details set waste_name=#{wasteName},waste_number=#{wasteNumber},waste_price=#{wastePrice} " +
            "where waste_order_details_id=#{wasteOrderDetailsId};")
    void updateWasteOrderDetails1(WasteOrderDetailsVo wasteOrderDetailsVo);

    @Update("update t_waste_order set waste_order_status=#{wasteOrderStatus} where waste_order_id=#{wasteOrderId};")
    void updateWasteOrder1(WasteOrderVo1 wasteOrderVo1);

    @Select("select * from t_waste_order where waste_order_id=#{wasteOrderId}")
    WasteOrder findOne1(String wasteOrderId);
}
